package com.itheima.EX;


import com.itheima.tools.OneBook;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


/**
 * 小说排行榜的下载案例
 */
public class TOP_DOWN {
    public static void main(String[] args) throws Exception {


    /*
        测试一下地址
        div.box:nth-child(2) > h3:nth-child(1)
        #main > div:nth-child(3) > ul:nth-child(2) > li:nth-child(2) > a
        div.box:nth-child(2) > ul:nth-child(2) > li:nth-child(2) > a:nth-child(1)
        div.box:nth-child(2) > ul:nth-child(2) > li:nth-child(3) > a:nth-child(1)
        div.box:nth-child(2) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)
     */
    /*
        确定要爬的是一个排行榜修真,仙侠
     */
        //地址确定
        String indexUrl = "http://www.xbiquge.la/";
            //解析数据 获取document对象
        String html = clickUrl(indexUrl);
        Document document = Jsoup.parse(html);
        String paihangurl =indexUrl+ document.select("#wrapper > div.nav > ul > li:nth-child(9) > a").attr("href");
//        System.out.println(paihangurl);
        //获取到排行榜详情页面的html
        String paihangneirong = clickUrl(paihangurl);
        //解析详情也数据 documentXQ
        Document documentXQ = Jsoup.parse(paihangneirong);
//        String bookUrl = documentXQ.select("#main > div:nth-child(3) > ul:nth-child(2) ")./*text()*/attr("href");
//        System.out.println(bookUrl);
        /*
            获取多个标签的方法如下
         */
        Elements hrefs = documentXQ.select("#main > div:nth-child(3) > ul:nth-child(2) a[href*=http://www.xbiquge.la]");
        for(Element elem:hrefs){
//            System.out.println(elem.text());
            System.out.println(elem.attr("href"));
            //获取到了全部的链接
            String herf = elem.attr("href");

            //进入每一个url,并且返回html
            String htmls = clickUrl(herf);
            //解析当前的 单本小说的目录页
            Document OneDoc = Jsoup.parse(htmls);
            //获取第一张的url
            String hrefForOen = OneDoc.select("#list > dl > dd:nth-child(1) > a").attr("href");
            System.out.println(hrefForOen);
            String OneBookDownUrl = "http://www.xbiquge.la"+hrefForOen;
            OneBook.downOneBook(OneBookDownUrl);


        }


//        if (bookUrl != "####" || bookUrl != "/xiuzhenxiaoshuo/"){
//
//        }

    }

    //封装一个访问链接的工具类
    public static String clickUrl(String url) throws Exception {
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置请求方式GET
        HttpGet httpGet = new HttpGet(url);
        //设置请求参数
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36");
        //发送请求,获取数据
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String html = EntityUtils.toString(response.getEntity(), "UTF-8");

        return html;
    }
}
